IF exists(SELECT * FROM dbo.sysobjects WHERE name='stamboekVervangEinddatum' AND xtype='P') 
BEGIN
	DROP PROCEDURE stamboekVervangEinddatum
END
GO

create  procedure stamboekVervangEinddatum(@oud varchar(10),
@nieuw varchar(10),@schooljaar varchar(4),@school char(6))
as
declare @datumOud dateTime
declare @datumNieuw dateTime 

set @datumOud = convert(dateTime,@oud,103)
set @datumNieuw = convert(dateTime,@nieuw,103)

update gestartVerlaten
  set verlaten = @datumNieuw
  from gestartVerlaten as g
	right join (select * from historiek
	   where left(stamboekNr,2) = right(@schooljaar,2)
	   and schoolNummer = @school) as h
	on h.inschrijvingsNr = g.inschrijvingsNr
	and h.schoolNummer = g.schoolNummer
  where g.verlaten between @datumOud and dateadd(day,1,@datumOud)